var app = getApp()

Page({
  data:{
    activities : [],
    currentPage : 1,
    pageSize : 20,
    hideNoData : true,
    hideBottomLoading : true
  },
  onLoad:function(options){
    this.loadData();
  },

  initScrollHeight : function(){
    var that = this;
    wx.getSystemInfo({
          success:function(res){
              that.setData({
                  scrollHeight:res.windowHeight
              });
          }
      });
  },

  loadData : function(){
    if(!this.data.hideBottomLoading) return;
    var that = this;
    if(this.data.currentPage == 1){
      app.showLoading();
    }else{
      this.setData({
        hideBottomLoading : false
      })
    }
    var data = {"pageSize":this.data.pageSize, "currentPage":this.data.currentPage};  
    app.request("task/user/activity/history", "POST", data, function(res){
        if(res.result.content.length == 0){
          that.setData({
            hideNoData : false,
            hideBottomLoading : true
          });
          return;
        }
        for(var i = 0;i<res.result.content.length;i++){
          res.result.content[i].status = JSON.parse(res.result.content[i].status);
        }

        if(that.data.currentPage == 1){
          that.initScrollHeight();
          app.closeLoading();
        }
 
        that.setData({
          activities : that.data.activities.concat(res.result.content),
          currentPage : that.data.currentPage + 1,
          hideBottomLoading : true
        });
        
    });
  },

  loadMore : function(){
    this.loadData();
  },

  onActivityClick : function(event){
     var id = event.currentTarget.dataset.hi;
      wx.navigateTo({
          url: '../activity/activity?id='+id
      })
  }
})